package com.buss.article.controller;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.jweb.core.bean.PageMode;
import org.jweb.core.bean.ReplyDataMode;
import org.jweb.core.query.hibernate.qbc.CriteriaQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.buss.admin.service.IArticleCatService;
import com.buss.article.trans.form.ArticleSearchForm;
import com.buss.article.trans.vo.ArticleVO;
import com.buss.article.utils.ToolsUtil;
import com.buss.common.entity.EasyUITreeNode;
import com.buss.common.entity.HxArticle;
import com.buss.common.entity.HxArticleCat;
import com.buss.common.service.SysServiceI;

@Controller
@RequestMapping("/article")
public class ArticleController {

	@Autowired
	private SysServiceI sysService;

	@RequestMapping("/list.do")
	@ResponseBody
	public ReplyDataMode articleList(ArticleSearchForm searchForm, HttpServletRequest request) {
		ReplyDataMode dataMode = new ReplyDataMode();
		CriteriaQuery criteriaQuery = searchForm.getCriteriaQuery();

		Map<String, Object> orderMap = new HashMap<>();
		orderMap.put("createTime", "desc");
		orderMap.put("updateTime", "desc");
		criteriaQuery.setOrder(orderMap);

		List<HxArticle> articles = this.sysService.list(criteriaQuery);

		// 封装返回数据
		List<ArticleVO> articleVOs = new ArrayList<>();
		ArticleVO articleVO = null;
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		for (HxArticle ha : articles) {
			articleVO = new ArticleVO();
			articleVO.copyEntity(ha);

			
			  if(ha.getPublishTime() != null) {
			  articleVO.setPublishTime2(dateFormat.format(ha.getPublishTime())); }
			  
			 /* if(ha.getUpdateTime() != null) {
			 * articleVO.setUpdateTime(dateFormat.format(ha.getUpdateTime())); }
			 */
			articleVOs.add(articleVO);
		}

		dataMode.setData(articleVOs);
		dataMode.setStatusCode("200");
		dataMode.setSuccess(true);

		return dataMode;
	}

	@RequestMapping("/pagelist.do")
	@ResponseBody
	public ReplyDataMode articlePageList(ArticleSearchForm searchForm, HttpServletRequest request) throws Exception {
		ReplyDataMode dataMode = new ReplyDataMode();
		CriteriaQuery criteriaQuery = searchForm.getCriteriaQuery();

		Map<String, Object> orderMap = new HashMap<>();
		orderMap.put("createTime", "desc");
		orderMap.put("updateTime", "desc");
		criteriaQuery.setOrder(orderMap);

		int pageNo = Integer.parseInt(searchForm.getPageNo());
		int pageSize = Integer.parseInt(searchForm.getPageSize());

		PageMode mode = this.sysService.list4page(criteriaQuery, pageNo, pageSize);
		List<HxArticle> articles = mode.getResult();

		// 封装返回数据
		List<ArticleVO> articleVOs = new ArrayList<>();
		ArticleVO articleVO = null;
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		for (HxArticle ha : articles) {
			articleVO = new ArticleVO();
			articleVO.copyEntity(ha);

			
			 if(ha.getPublishTime() != null) {
			 articleVO.setPublishTime2(dateFormat.format(ha.getPublishTime())); }
			 
			 /* if(ha.getUpdateTime() != null) {
			 * articleVO.setUpdateTime(dateFormat.format(ha.getUpdateTime())); }
			 */
			articleVOs.add(articleVO);
		}
		mode.setResult(articleVOs);

		dataMode.setData(mode);
		dataMode.setStatusCode("200");
		dataMode.setSuccess(true);
		dataMode.setAttribute(ToolsUtil.convertBean(searchForm));

		return dataMode;
	}

	@RequestMapping("/page.do")
	@ResponseBody
	public ReplyDataMode page(ArticleSearchForm searchForm, HttpServletRequest request) throws Exception {
		ReplyDataMode dataMode = new ReplyDataMode();
		// 搜索条件回显
		String keyword = "";
		if (searchForm.getTitle() != null && !"".equals(searchForm.getTitle())) {
			keyword = searchForm.getTitle();
		}
		// 手动加条件
		searchForm.setStatus("1"); // 1正常
		CriteriaQuery criteriaQuery = searchForm.getCriteriaQuery();

		Map<String, Object> orderMap = new HashMap<>();
		orderMap.put("createTime", "desc");
		orderMap.put("updateTime", "desc");
		criteriaQuery.setOrder(orderMap);

		int pageNo = Integer.parseInt(searchForm.getPageNo());
		int pageSize = Integer.parseInt(searchForm.getPageSize());

		PageMode mode = this.sysService.list4page(criteriaQuery, pageNo, pageSize);
		List<HxArticle> articles = mode.getResult();

		// 封装返回数据
		List<ArticleVO> articleVOs = new ArrayList<>();
		ArticleVO articleVO = null;
		 SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		for (HxArticle ha : articles) {
			articleVO = new ArticleVO();
			articleVO.copyEntity(ha);
			if (!"".equals(keyword)) {
				// 搜索 替换关键字
				articleVO.setTitle(unescapeStr(articleVO.getTitle(), keyword, "<span>" + keyword + "</span>"));
			}
			
			 if(ha.getCreateTime() != null) {
			 articleVO.setPublishTime2(dateFormat.format(ha.getPublishTime())); }
			  
			 /* if(ha.getUpdateTime() != null) {
			 * articleVO.setUpdateTime(dateFormat.format(ha.getUpdateTime())); }
			 */
			articleVOs.add(articleVO);
		}
		mode.setResult(articleVOs);

		dataMode.setData(mode);
		dataMode.setStatusCode("200");
		dataMode.setSuccess(true);
		Map<String, Object> attribute = ToolsUtil.convertBean(searchForm);
		attribute.put("keyword", keyword);
		dataMode.setAttribute(attribute);

		return dataMode;
	}

	public static final String unescapeStr(String str, String regex, String replacement) {
		if (isNullValue(str))
			return "";
		str = str.replaceAll(regex, replacement);
		return str;
	}

	public static boolean isNullValue(String value) {
		return (value == null) || (value.trim().equals(""));
	}

	/**
	 * 获取文章的上一篇，下一篇
	 */
	@RequestMapping("/relationlist.do")
	@ResponseBody
	public ReplyDataMode articleRelationList(ArticleSearchForm searchForm, HttpServletRequest request) {
		long id = Long.parseLong(searchForm.getId());
		searchForm.setId(null);
		ReplyDataMode dataMode = new ReplyDataMode();
		List<HxArticle> articles1 = this.getPreArtivle(searchForm, request, id);
		List<HxArticle> articles2 = this.getNextArtivle(searchForm, request, id);
		// 封装返回数据
		Map<String, Object> temp = new HashMap<>();
		ArticleVO pre = new ArticleVO();
		if (articles1 != null && articles1.size() > 0) {
			pre.copyEntity(articles1.get(0));
		} else {
			pre = (null);
		}
		ArticleVO next = new ArticleVO();
		if (articles2 != null && articles2.size() > 0) {
			next.copyEntity(articles2.get(0));
		} else {
			next = (null);
		}

		temp.put("pre", pre);
		temp.put("next", next);

		dataMode.setData(temp);
		dataMode.setStatusCode("200");
		dataMode.setSuccess(true);

		return dataMode;
	}

	private List getPreArtivle(ArticleSearchForm searchForm, HttpServletRequest request, Long id) {

		CriteriaQuery criteriaQuery = searchForm.getCriteriaQuery();

		Map<String, Object> orderMap = new HashMap<>();
//		orderMap.put("updateTime", "desc");
		orderMap.put("createTime", "desc");
		criteriaQuery.setOrder(orderMap);
		// 上一篇
		criteriaQuery.lt("id", id);
		criteriaQuery.add();
		
		List<HxArticle> articles1 = this.sysService.list(criteriaQuery);
		return articles1;
	}

	private List getNextArtivle(ArticleSearchForm searchForm, HttpServletRequest request, Long id) {

		CriteriaQuery criteriaQuery = searchForm.getCriteriaQuery();

		Map<String, Object> orderMap = new HashMap<>();
//		orderMap.put("updateTime", "desc");
		orderMap.put("createTime", "asc");
		criteriaQuery.setOrder(orderMap);
		// 上一篇
		criteriaQuery.gt("id", id);
		criteriaQuery.add();
		List<HxArticle> articles1 = this.sysService.list(criteriaQuery);
		return articles1;
	}

}
